<template>
  <div class="plcDevice-list">
     <el-page-header @back="goBack" >
    </el-page-header>
    <div style="height: 60px">
      <el-button type="primary" @click="addPlcDeviceData" style="margin: 5px;float: right">新增</el-button>
      <el-button type="primary" @click="addPlcPoint" style="margin: 5px;float: right">管理plc点位</el-button>
    </div>
    <el-table
      stripe
      height="800"
      highlight-current-row
      @current-change="handleCurrentChange"
      :data="deviceDataList"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="plc设备编号"
        width="350"
        prop="device_no"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="plc设备名称"
        width="350"
        prop="device_name"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="行政区划"
        prop="dtu_code"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="地址"
        prop="dtu_addr"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="监控地址"
        prop="video_address"
      >
      </el-table-column>
      <el-table-column
        align="center"
        width="200"
        label="操作"
        prop="handler"
      >
        <template slot-scope="{row}">
          <span @click="editDeviceClick(row)"><svg-icon class="handler-icon" icon-class="edit"/></span>
          <span @click="delDeviceClick(row.device_id)"><svg-icon class="handler-icon" icon-class="right"/></span>
        </template>
      </el-table-column>
    </el-table>
    <plcDevice-form ref='plcDevice_from_dialog'/>
  </div>
</template>

<script>
  import PlcDeviceForm from './PlcDeviceForm'

  export default {
    name: "plcDeviceList",
    components: {PlcDeviceForm},
    methods: {
      queryData(){
        let me = this;
        this.$axios.post('deviceRoutes', {
          action_type: 'plcDeviceQuery',
          enterprise_id:me.$route.params.enterprise_id
        }).then(function (response) {
          me.deviceDataList = response.data.data;
          return {success : true}
        });
      },
      editDeviceClick(record) {
        let formDate = JSON.parse(JSON.stringify(record));//由于vue双向绑定的缘故，form表单中数据变换会营销到表格中的数据同时变换，因此这里需要进行一次转换操作避免绑定情况出现
        this.$refs.plcDevice_from_dialog.setForm(formDate);
      },

      delDeviceClick(device_id) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let me = this;
          this.$axios.post('deviceRoutes',{
            action_type: 'plcDeviceDel',
            device_id:device_id,
          }).then(function (response) {
            if(response.data.success) me.$message({type: 'success',message: '删除成功!'});
            else me.$message({type: 'error',message: '删除失败!'});
            me.queryData();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      addPlcDeviceData() {
        this.$refs.plcDevice_from_dialog.setForm();
      },
      goBack() {
        this.$router.push({name: 'Enterprise'});
      },
      addPlcPoint() {
        if(this.currentRow){
          this.$router.push({name: 'plcPointList', params: {device_id: this.currentRow.device_id,enterprise_id: this.$route.params.enterprise_id}});//push是跳转页面 params传值过去
        }else{
          alert('请选择需要添加plc点位的设备')
        }
      },
      handleCurrentChange(val) {
        this.currentRow = val;
      }
    },
    data() {
        return {
          deviceDataList : [],
        }
    },
    created() {
      let me = this;
      if(me.$route.params.enterprise_id){
        me.queryData();
      }else{
        me.$router.push({name: 'user'});
      }
    },

  }
</script>

<style lang="scss" scoped>
  .plcDevice-list {
    background-color: #fff;
  }
</style>
